﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WCFAuthorization.Service;

namespace WCFAuthorization.Server
{
    public delegate void NewUserHandler(User user);

    public partial class FormAddUser : Form
    {
        public FormAddUser()
        {
            InitializeComponent();
        }

        private void FormAddUser_Load(object sender, EventArgs e)
        {
            TimePicker.Value = DateTime.Now.AddMonths(1);
            ckEnable.Checked = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCode.Text))
            {
                MessageBox.Show("请填写正确信息！");
                return;
            }

            var query = Cacher.UsersCache.Where(p => p.UserName.Equals(txtCode.Text));
            if (query.Count() > 0)
            {
                MessageBox.Show("用户已存在!");
                return;
            }

            User newUser=new User(
                txtCode.Text,
                TimePicker.Value,
                ckEnable.Checked,
                txtTag.Text);
            if (_newUser != null)
                _newUser(newUser);
            MessageBox.Show("添加成功!");
            this.Close();

        }

        event NewUserHandler _newUser=null;

        public event NewUserHandler NewUser
        {
            add
            {
                _newUser += value;
            }
            remove
            {
                _newUser -= value;
            }
        }
    }
}
